<?php
/**
 * 客户联系人
 */
require_once("../global.inc.php");

class SysDistributeMemClass extends SystemFrameWork{
	var $obj_project_member;
	/**
	 * 项目对象
	 *
	 * @var obj
	 */
	var $obj_project;
	/**
	 * 部门对象
	 *
	 * @var obj
	 */
	var $obj_department;
	var $obj_project_verify;
	/**
	 * 验证对象
	 *
	 * @var obj
	 */
	var $obj_validate;
	/**
	 * 分页对象
	 *
	 * @var obj
	 */
	var $obj_page;
	
	function main(){
		if (!is_object($this->obj_project_member)){
			require_once ("project_member.class.php");
			$this->obj_project_member = new ProjectMemberClass();
		}
		
		if (!is_object($this->obj_project)){
			require_once ("project.class.php");
			$this->obj_project = new ProjectClass();
		}
		/**
		 * 部门
		 */
		if (!is_object($this->obj_department)){
			require_once('department.class.php');
			$this->obj_department = new DepartmentClass();
		}
		if (!is_object($this->obj_project_verify)){
			require_once ("project_verify.class.php");
			$this->obj_project_verify = new ProjectVerifyClass();
		}
		/**
		 * 创建验证对象
		 */
		if (!is_object($this->obj_validate)){
			require_once("commonvalidate.class.php");
			$this->obj_validate = new CommonValidate();
		}
		/**
		 * 初始化分页类
		 */
		if (!is_object($this->obj_page)){
			require_once("commonpage.class.php");
			$this->obj_page = new CommonPage();
		}
		
		switch($this->_input['action']){
			case "ajax_list":
				$this->_ajax_list();
				break;
			case "add":
				$this->_add();
				break;
	       case "view":
				$this->_view();
				break;
			case "save":
				$this->_save();
				break;
			case "modi":
				$this->_modi();
				break;
			case "update":
				$this->_update();
				break;
			case "del":
				$this->_del();
			    break;
			case "make_cost" : 
			     $this->_make_cost();
			     break;
			case "update_cost" : 
			     $this->_update_cost();
			     break;
			case "cost_view":
			    $this->_cost_view();
			    break;
			case 'caiwu' :
			    $this->_caiwu();
			    break;
			case 'caiwu_view':
			    $this->_caiwu_view();
			    break;
			case "zonghe" :
			    $this->_zonghe();
			    break;
			case "zonghe_view" :
			    $this->_zonghe_view();
			    break;
			case "submit":
			    $this->_submit();
			    break;
			case "submit_view":
			    $this->_submit_view();
			    break;
			default:
				$this->_list();
		}
	}
	/**
	 * 列表
	 */
	function _list(){
		$this->_ajax_list();
	}
	
	/**
	 * ajax类表
	 */
	function _ajax_list(){
	}
	/**
	 * 信息添加
	 */
	function _add(){
		/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'项目ID不能为空'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}
		//取信息
		$project_array = $this->obj_project->getProject($this->_input["id"]);
		//获取部门信息
		$dept_array = $this->obj_department->getDepartmentList('', $obj_page);
		
		$where = 'where project_member.project_id='.$this->_input['id'].' and project_member.status=1';
		$member_array = $this->obj_project_member->listProjectMember($where, $obj_page);
		
		if ($member_array) {
			foreach ($member_array as $k => $member) {
				foreach ($dept_array as $dept){
					if($dept['id'] == $member['dept_id']){
						$member_array[$k]['dept_name'] = $dept['name'];
					}
				}
				//格式化日期
				if ($member['start_date']) {
					$member_array[$k]['start_date'] = date('Y-m-d', $member['start_date']);
				}else{
					$member_array[$k]['start_date'] = date('Y-m-d');
				}
				if ($member['end_date']) {
					$member_array[$k]['end_date'] = date('Y-m-d', $member['end_date']);
				}else{
					$member_array[$k]['end_date'] = date('Y-m-d');
				}
			}
		}
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->_input['curpage']);
		$this->output('member_array',$member_array);
		$this->output('project_array',$project_array);
		$this->output('dept_array',$dept_array);
		$this->showpage('distribute_mem.add');
	}
	/**
	 * 信息添加
	 */
	function _save(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["txtProjectName"], "require"=>"true", "message"=>'项目名称不能为空'),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong'])
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			
			if (is_array($this->_input["memberlist2"])) {
				foreach ($this->_input["memberlist2"] as $item) {
					if ($item) {
						$value_array = array();
						$value_array['project_id'] = $this->_input["project_id"];  //项目ID
						$value_array['member_id'] = $item;  //员工ID
						$value_array['start_date'] = '0';  //开始日期
						$value_array['end_date'] = '0';  //结束日期
						$value_array['scale'] = '0';  //权重
						$value_array['role_id'] = '0';  //角色
						$value_array['user_id'] = $_SESSION['s_admin']['id'];  //提交人
						$value_array['create_time'] = time();  //提交时间
						$value_array['status'] = 1;  //状态
						$result = $this->obj_project_member->addProjectMember($value_array);
					}
				}
			}
			
			if ($result != true){
				$this->redirectPath('error','','添加失败',1);
			}else {
				
				//记录操作日志
				SystemPowerClass::addSysLog('项目->预立项管理->提交分配人员表');
				$this->redirectPath('error','./distribute_member.manage.php?action=add&id='.$this->_input["project_id"].'&curpage=1','添加成功',1);
			}
//		}
		
	}
	
	/**
	 * 保存修改
	 */
	function _update(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["txtProjectName"], "require"=>"true", "message"=>'项目名称不能为空'),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong'])
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			if (is_array($this->_input["member_id"])) {
				$ids = $this->_input["member_id"];
				for($i=0; $i<count($ids); $i++) {
					if ($ids[$i]) {
						$value_array = array();
						$value_array['id'] = $ids[$i];  //ID
						$value_array['start_date'] = strtotime($this->_input["start_date"][$i]);  //开始日期
						$value_array['end_date'] = strtotime($this->_input["end_date"][$i]);  //结束日期
						$value_array['scale'] = $this->_input["scale"][$i];  //权重
						$value_array['role_id'] = $this->_input["role_id"][$i];  //角色
						$result = $this->obj_project_member->updateProjectMember($value_array);
					}
				}
			}
			
			if ($result != true){
				$this->redirectPath('error','','提交失败',1);
			}else {
				
				$flow_array = include('flow_config.ini.php');
				$roles = $flow_array['2']['roles'];
				if (is_array($roles)) {
					$project_id = $this->_input["project_id"];
					
					$tmp_array = array();
					$tmp_array['id'] = $project_id;
					$tmp_array['is_submit'] = '1';
					$tmp_array['project_step'] = '2';
					
					$i = 0;
					foreach ($roles as $val){
						$value_array = array();
						$value_array['project_id'] = $project_id;
						$value_array['verify_flow'] = '2';
						$value_array['verify_role'] = $val;
						$value_array['assign_time'] = time();
						$value_array['member_id'] = '0';
						$value_array['verify_time'] = '0';
						$value_array['verify_options'] = '';
						$value_array['verify_status'] = '0';
						$result = $this->obj_project_verify->addProjectVerify($value_array);
						//下审批流程
						if ($i == 0) {
							$newid = mysql_insert_id();
							$tmp_array['nextflow_id'] = $newid;
						} 
						$i++;
					}
					$this->obj_project->updateProject($tmp_array);
				}
					
				//记录操作日志
				SystemPowerClass::addSysLog('项目->预立项管理->提交分配人员表');
				$this->redirectPath('error','./project_official.manage.php?action=list'.$this->_input["project_id"].'&curpage=1','提交成功',1);
			}
//		}
	}
	
	/**
	 * 删除信息
	 */
	function _del(){

	}
	
    public function _view()
    {
        $this->showpage('distribute_mem.view');
    }
    
    public function _make_cost()
    {
    	/**
		 * 信息验证
		 */
		$this->obj_validate->validateparam = array(
			array("input"=>$this->_input["id"], "require"=>"true","validator"=>"Number", "message"=>'项目ID不能为空'),
		);
		$error = $this->obj_validate->validate();
		if ($error != ''){
			$this->redirectPath('error','',$error,1);
		}
		//取信息
		$project_array = $this->obj_project->getProject($this->_input["id"], 'more');
		
		$where = 'where project_member.project_id='.$this->_input['id'].' and project_member.status=1';
		$member_array = $this->obj_project_member->listProjectMember($where, $obj_page);
		
		if ($member_array) {
			foreach ($member_array as $k => $member) {
				//工作天数
				$member_array[$k]['days'] = intval(($member['end_date'] - $member['start_date']) / 86400);
				$member_array[$k]['day_gongzi'] = 0;
				if ($member['gongzi'] > 0) {
					$member_array[$k]['day_gongzi'] = intval($member['gongzi']/22);
				}
				$member_array[$k]['total_gongzi'] = $member_array[$k]['days'] * $member_array[$k]['day_gongzi'];
			}
		}
		/**
		 * 页面输出
		 */
		$this->output("curpage", $this->_input['curpage']);
		$this->output('member_array',$member_array);
		$this->output('project_array',$project_array);
        $this->showpage('distribute_mem.make_cost');
    }
    
    /**
	 * 保存修改
	 */
	function _update_cost(){
		/**
		 * 信息验证
		 */
//		$this->obj_validate->validateparam = array(
//			array("input"=>$this->_input["txtProjectName"], "require"=>"true", "message"=>'项目名称不能为空'),
//			array("input"=>$this->_input["news_c_id"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsClassIsEmpty']),
//			array("input"=>$this->_input["txtsort"], "require"=>"true","validator"=>"Number", "message"=>$this->_lang['errSysNNewsSortIsWrong'])
//		);
//		$error = $this->obj_validate->validate();
//		if ($error != ''){
//			$this->redirectPath('error','',$error,1);
//		}else {
			$curflow = '3';
			$flow_array = include('flow_config.ini.php');
			$roles = $flow_array["$curflow"]['roles'];
			if (is_array($roles)) {
				$project_id = $this->_input["project_id"];
				
				$tmp_array = array();
				$tmp_array['id'] = $project_id;
				$tmp_array['is_submit'] = '1';
				$tmp_array['project_step'] = $curflow;
				
				$i = 0;
				foreach ($roles as $val){
					$value_array = array();
					$value_array['project_id'] = $project_id;
					$value_array['verify_flow'] = $curflow;
					$value_array['verify_role'] = $val;
					$value_array['assign_time'] = time();
					$value_array['member_id'] = '0';
					$value_array['verify_time'] = '0';
					$value_array['verify_options'] = '';
					$value_array['verify_status'] = '0';
					$result = $this->obj_project_verify->addProjectVerify($value_array);
					//下审批流程
					if ($i == 0) {
						$newid = mysql_insert_id();
						$tmp_array['nextflow_id'] = $newid;
					} 
					$i++;
				}
				$this->obj_project->updateProject($tmp_array);
			}
				
			//记录操作日志
			SystemPowerClass::addSysLog('项目->预立项管理->生成人员成本表');
			$this->redirectPath('error','./project_official.manage.php?action=list'.$this->_input["project_id"].'&curpage=1','提交成功',1);
		
//		}
	}
    public function _cost_view()
    {
        $this->showpage('distribute_mem.cost_view');
    }
    
    public function _caiwu()
    {
        $this->showpage('distribute_mem.caiwu');
    }
    
    public function _caiwu_view()
    {
        $this->showpage('distribute_mem.caiwu_view');
    }
    
    public function _zonghe()
    {
        $this->showpage('distribute_mem.zonghe');
    }
    
    public function _zonghe_view()
    {
        $this->showpage('distribute_mem.zonghe_view');
    }

    public function _submit()
    {
        $this->showpage('distribute_mem.submit');
    }
    
    public function _submit_view()
    {
        $this->showpage('distribute_mem.submit_view');
    }
}
$distribute_mem = new SysDistributeMemClass();
$distribute_mem->main();
unset($customer);
?>